<template>
  <div class="app-container">
    <el-row>
      <el-col :span="8">
        <el-card class="box-card" style="height: 800px">
          <el-button
            v-hasPermi="['system:seq:add']"
            icon="el-icon-plus"
            plain
            size="mini"
            type="primary"
            @click="handleAdd"
          >新增规则
          </el-button>

          <el-table v-loading="loading" :data="seqList" highlight-current-row @row-click="seqRowClick">
            <el-table-column v-if="false" align="center" label="主键" prop="id"/>
            <el-table-column align="center" label="名称" prop="seqName"/>
            <!--            <el-table-column label="关联业务" align="center" prop="seqCode"/>-->
            <el-table-column align="center" label="分隔符" prop="delimiter"/>
            <el-table-column align="center" label="重置规则" prop="resetRule"/>
            <!--            <el-table-column label="当前序号" align="center" prop="currentNo"/>-->
            <el-table-column :formatter="stateFormatter" align="center" label="状态" prop="active"/>
            <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
              <template slot-scope="scope">
                <el-tooltip class="item" content="开启" effect="dark" placement="bottom">
                  <el-button v-hasPermi="['system:seq:edit']" icon="el-icon-coordinate" size="mini" type="text"
                             @click="handleActive(scope.row)"
                  />
                </el-tooltip>
                <el-tooltip class="item" content="修改" effect="dark" placement="bottom">
                  <el-button v-hasPermi="['system:seq:edit']" icon="el-icon-edit" size="mini" type="text"
                             @click="handleUpdate(scope.row)"
                  />
                </el-tooltip>
                <el-tooltip class="item" content="删除" effect="dark" placement="bottom">
                  <el-button v-hasPermi="['system:seq:remove']" icon="el-icon-delete" size="mini" type="text"
                             @click="handleDelete(scope.row)"
                  />
                </el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="16">
        <el-button v-hasPermi="['system:seq:edit']" icon="el-icon-files" size="mini" style="margin-left: 16px"
                   @click="handleSaveDetail"
        >保存
        </el-button>
        <div style="height: 800px; position: absolute; top: 40%; display: inline-flex">
          <el-card v-for="(item, index) in seqDetails" class="box-card"
                   style="width: 200px; height: 120px; margin-left: 5px; position: relative"
          >
            <span>{{ seqRuleFormat(item) }}</span>
            <el-input v-model="item.value" :disabled="item.code !== '5'" style="margin-top: 10px"></el-input>
            <i class="el-icon-circle-close" style="position: absolute; right: 4px; top: 4px; z-index: 100"
               @click="delDetail(index)"
            ></i>
          </el-card>
          <el-button :disabled="!selectSeq || Object.keys(selectSeq).length === 0"
                     icon="el-icon-plus"
                     style="width: 56px; height: 120px; margin-left: 5px"
                     type="primary"
                     @click="addNewDetail"
          />
        </div>
      </el-col>
    </el-row>

    <!-- 添加或修改编码规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="编码规则名称" prop="seqName">
          <el-input v-model="form.seqName" placeholder="请输入编码规则名称"/>
        </el-form-item>
        <!--        <el-form-item label="关联业务编码" prop="seqCode">-->
        <!--          <el-input v-model="form.seqCode" placeholder="请输入关联业务编码" />-->
        <!--        </el-form-item>-->
        <el-form-item label="分隔符" prop="delimiter">
          <el-input v-model="form.delimiter" placeholder="默认分隔符: -"/>
        </el-form-item>
        <el-form-item label="重置规则" prop="resetRule">
          <el-input v-model="form.resetRule" placeholder="流水号重置规则 单位为天"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加规则详情 -->
    <el-dialog :visible.sync="detailOpen" append-to-body title="添加规则详情" width="500px">
      <el-form ref="form" :model="detailForm" :rules="detailRules" label-width="80px">
        <el-form-item label="规则名称" prop="detail">
          <el-select
            v-model="detailForm.detail"
            clearable
            placeholder="规则名称"
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="dict in seqRules"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDetailForm">确 定</el-button>
        <el-button @click="detailOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { activeSeq, addSeq, delSeq, getSeq, listSeq, updateSeq } from '@/api/system/seq'

export default {
  name: 'Seq',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 编码规则表格数据
      seqList: [],
      // 选中的编码规则
      selectSeq: undefined,
      // 编码规则详情数组
      seqDetails: [],
      // 编码规则类型
      seqRules: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 添加规则详情弹出层
      detailOpen: false,
      // 规则详情条目表单
      detailForm: {},
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        seqName: [
          { required: true, message: '编码规则名称不能为空', trigger: 'blur' }
        ]
      },
      detailRules: {
        detail: [
          { required: true, message: '规则不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('seq_rule_type').then(response => {
      this.seqRules = response.data
    })
  },
  methods: {
    /** 查询编码规则列表 */
    getList() {
      this.loading = true
      listSeq(this.queryParams).then(response => {
        this.seqList = response.data
        this.loading = false
      })
    },
    // 新增规则详情按钮点击事件
    addNewDetail() {
      this.resetDetail()
      this.detailOpen = true
    },
    // 保存规则详情
    handleSaveDetail() {
      this.selectSeq.seqDetail = JSON.stringify(this.seqDetails)
      updateSeq(this.selectSeq).then(response => {
        this.msgSuccess('保存成功')
        this.selectSeq = undefined
        this.seqDetails = []
      })
    },
    // 点击某一行事件
    seqRowClick(row, column, event) {
      this.selectSeq = undefined
      this.seqDetails = []
      this.selectSeq = row
      this.seqDetails = JSON.parse(this.selectSeq.seqDetail)
    },
    // 删除规则详情
    delDetail(index) {
      this.seqDetails.splice(index, 1)
    },
    // 提交规则详情对话框
    submitDetailForm() {
      let detail = {}
      detail.code = this.detailForm.detail
      detail.value = ''
      this.seqDetails.push(detail)
      this.detailOpen = false
    },
    // 规则详情名称 字典翻译
    seqRuleFormat(row) {
      return this.selectDictLabel(this.seqRules, row.code)
    },
    // 是否开启状态
    stateFormatter(row, column) {
      return row.active === '0' ? '' : '开启'
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        seqName: undefined,
        seqDetail: undefined,
        seqCode: undefined,
        delimiter: undefined,
        resetRule: undefined,
        currentNo: undefined,
        active: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      }
      this.resetForm('form')
    },
    resetDetail() {
      this.detailForm = {
        detail: undefined
      }
      this.resetForm('detailForm')
    },
    // 开启规则
    handleActive(row) {
      activeSeq(row).then(response => {
        this.msgSuccess('开启成功')
        this.getList()
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加编码规则'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSeq(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改编码规则'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSeq(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSeq(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除编码规则编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSeq(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
<style scoped>
.el-table__body tr.current-row > td {
  background-color: #69A8EA !important;
  color: #fff;
}
</style>
